package cn.nexd.app.guestsystem.database;

import android.content.ContentValues;
import android.database.Cursor;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.nexd.app.guestsystem.application.GuestSystemApplication;
import cn.nexd.framework.error.NexdCrashException;
import cn.nexd.framework.util.Logger;
import cn.nexd.sdk.collector.NexdCollectorResult;

/**
 * Created by wangxu on 16/8/30.
 */
public class DatabaseUtil {

    private static final String TAG = "DatabaseUtil";

    public static void insert(String tableName, String nullColumn, ContentValues values) {
        GuestSystemApplication.helper.getWritableDatabase().insert(tableName, nullColumn, values);

    }

    public static void delete(String tableName, String where, String[] value) {
        GuestSystemApplication.helper.getWritableDatabase().delete(tableName, where, value);
    }

    public static void delete(String sql) {
        execSql(sql);
    }

    public static void insert(String sql) {
        execSql(sql);
    }

    public static void deleteAll(String tableName) {
        String sql = "delete if exist from " + tableName;
        execSql(sql);
    }

    public static void insertALLUser(JSONArray array) {
        ContentValues values = new ContentValues();

        for (int i = 0; i < array.length(); i++) {
            try {
                JSONObject jsonObject = array.getJSONObject(i);
                values.put("company", jsonObject.getString("company"));
                values.put("mac", jsonObject.getString("mac"));
                values.put("family_name", jsonObject.getString("family_name"));
                values.put("position", jsonObject.getString("position"));
                values.put("name", jsonObject.getString("name"));
                values.put("weight", jsonObject.getString("weight"));
                insert("user", null, values);
                values.clear();
            } catch (JSONException e) {
                e.printStackTrace();
                NexdCrashException.getInstance().reportException(e);
            }
        }
    }

    public static void insertAllMac(List<NexdCollectorResult> list) {
        for (NexdCollectorResult nexdCollectorResult : list) {
            String sql = "insert or ignore into mac (mac) values ('" + nexdCollectorResult.getSingleSourceAddress() + "')";
            execSql(sql);
        }

    }


    public static List<String> getAllMacAddress() {
        List<String> macAddress = new ArrayList<>();
        String sql = "select mac from mac";
        Cursor cursor = GuestSystemApplication.helper.getReadableDatabase().rawQuery(sql, null);
        if (cursor != null) {
            while (cursor.moveToNext()) {
                String mac = cursor.getString(cursor.getColumnIndex("mac"));
                if (!macAddress.contains(mac)) {
                    macAddress.add(mac);
                }
            }
        }

        if (cursor != null) {
            cursor.close();
        }

        return macAddress;
    }

    private static void execSql(String sql) {
        Logger.debug(TAG, sql);
        GuestSystemApplication.helper.getWritableDatabase().execSQL(sql);
    }
}
